package com.yunti.yntk.activity.stepper;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.*;
import android.widget.*;
import androidx.appcompat.widget.LinearLayoutCompat;
import com.google.android.material.textfield.TextInputEditText;
import com.yunti.yntk.R;
import com.yunti.yntk.activity.BaseActivity;
import com.yunti.yntk.activity.batchRecord.BatchRecordActivity;
import com.yunti.yntk.callback.PointPositionCallBack;
import com.yunti.yntk.manager.ComputScoreManager;
import com.yunti.yntk.manager.TotalStationManager;
import com.yunti.yntk.model.PointPosition;
import com.yunti.yntk.util.AngleUtils;

import java.io.IOException;

public class StepperActivity_tz extends BaseActivity implements View.OnClickListener, PointPositionCallBack {

    TextView step1Icon;
    TextView step1Text;
    TextView step2Icon;
    TextView step2Text;
    TextView step3Icon;
    TextView step3Text;
    LinearLayoutCompat steppercontent;
    Button prebutton;
    Button nextbutton;
    int pageindex = 1;
    int selectColor = Color.BLACK;
    int disselectColor = Color.GRAY;
    int mode;

    TextView resultValue;
    View step1;
    View step2;
    TextView hr_value;
    Button makezro;
    Button startSurvey;
    Spinner edmSpinner;
    TextInputEditText constant;
    TextView originaltext;
//    Button update;


    PointPosition apoint = new PointPosition();
    PointPosition bpoint = new PointPosition();
    private Integer subId;
    private String gameName;

    private int type;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initdata();
        initview();
    }

    @Override
    public int initLayoutId() {
        return R.layout.activity_stepper_qq;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() throws IOException {

    }

    void initdata() {
        ComputScoreManager.getInstance().setConstant(0);
        Intent intent = getIntent();
        this.gameName = intent.getStringExtra("subName");
        this.subId = intent.getIntExtra("subId", 0);
        this.type = intent.getIntExtra("type", 1);

        if (subId == 3 || subId == 13) {
            ComputScoreManager.getInstance().setGamemode(ComputScoreManager.QQ);
        }

        if (subId == 14) {
            ComputScoreManager.getInstance().setGamemode(ComputScoreManager.TB);
        }

        if (subId == 15) {
            ComputScoreManager.getInstance().setGamemode(ComputScoreManager.BQ);
        }

        constant = findViewById(R.id.constant);
        constant.setText(ComputScoreManager.getInstance().getConstant() + "");
        constant.addTextChangedListener(new TextWatcher() {
            //编辑框的内容发生改变之前的回调方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            //编辑框的内容正在发生改变时的回调方法 >>用户正在输入
            //我们可以在这里实时地 通过搜索匹配用户的输入
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            //编辑框的内容改变以后,用户没有继续输入时 的回调方法
            @Override
            public void afterTextChanged(Editable s) {
                String input = s.toString();
                try {
                    ComputScoreManager.getInstance().setConstant(Double.parseDouble(input));
                } catch (RuntimeException exception) {
                    exception.printStackTrace();
                    ComputScoreManager.getInstance().setConstant(0);
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    void initview() {
        step1Icon = findViewById(R.id.step1icon);
        step2Icon = findViewById(R.id.step2icon);

        step1Text = findViewById(R.id.step1text);
        step2Text = findViewById(R.id.step2text);

        steppercontent = findViewById(R.id.steppercontent);
        prebutton = findViewById(R.id.prebutton);
        prebutton.setOnClickListener(this);
        nextbutton = findViewById(R.id.nextbutton);
        nextbutton.setOnClickListener(this);

        edmSpinner = findViewById(R.id.edmSpanner);
        edmSpinner.setSelection((int) TotalStationManager.getInstance().edm);
        edmSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TotalStationManager.getInstance().setEdm(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        constant = findViewById(R.id.constant);
        constant.setText(ComputScoreManager.getInstance().getConstant() + "");
        constant.addTextChangedListener(new TextWatcher() {
            //编辑框的内容发生改变之前的回调方法
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            //编辑框的内容正在发生改变时的回调方法 >>用户正在输入
            //我们可以在这里实时地 通过搜索匹配用户的输入
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            //编辑框的内容改变以后,用户没有继续输入时 的回调方法
            @Override
            public void afterTextChanged(Editable s) {
                String aswer = s.toString();
                try {
                    ComputScoreManager.getInstance().setConstant(Double.parseDouble(aswer));
                } catch (RuntimeException exception) {
                    exception.printStackTrace();
                    ComputScoreManager.getInstance().setConstant(0);
                }
            }
        });

        buildstep1();
        buildstep2();
        addstep1();

        TotalStationManager.getInstance().setPointPositionCallBack(this);
    }

    void buildstep1() {
        step1 = LayoutInflater.from(this).inflate(R.layout.fragment_step1_qq, null);
        hr_value = step1.findViewById(R.id.HR_value);
        makezro = step1.findViewById(R.id.makezero);
        makezro.setOnClickListener(this);
    }

    void buildstep2() {
        step2 = LayoutInflater.from(this).inflate(R.layout.fragment_step3, null);
        startSurvey = step2.findViewById(R.id.startSurvey);
        startSurvey.setOnClickListener(this);
        resultValue = step2.findViewById(R.id.scoretext);

        originaltext = step2.findViewById(R.id.originaldata);
//        update = step2.findViewById(R.id.update);
//        update.setOnClickListener(this);
    }


    void addstep1() {
        step1Text.setTextColor(selectColor);
        step2Text.setTextColor(disselectColor);

        step1Icon.setText("1");
        step2Icon.setText("2");
        if (steppercontent.getChildCount() > 1) steppercontent.removeViewAt(0);
        steppercontent.addView(step1, 0);
    }

    void addstep2() {
        step1Text.setTextColor(disselectColor);
        step2Text.setTextColor(selectColor);

        step1Icon.setText("✔");
        step2Icon.setText("2");
        if (steppercontent.getChildCount() > 1) steppercontent.removeViewAt(0);
        steppercontent.addView(step2, 0);

    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.prebutton: {
                if (pageindex == 1) return;
                pageindex--;
                if (pageindex == 1) addstep1();
                if (pageindex == 2) addstep2();
                break;
            }
            case R.id.nextbutton: {
                if (pageindex == 2) {
                    Intent intent = new Intent(this, BatchRecordActivity.class);
                    intent.putExtra("subId", subId);
                    intent.putExtra("type", 1);
                    intent.putExtra("subName", gameName);
                    startActivity(intent);
                    this.finish();
                    return;
                }
                pageindex++;
                if (pageindex == 2) {
//                    if (mode==1||mode==2)ComputScoreManager.getInstance().computeH();
                    ComputScoreManager.getInstance().saveParameter();
                    addstep2();
                }
                break;
            }
            case R.id.startSurvey:
                new Thread() {
                    public void run() {
                        PointPosition result = TotalStationManager.getInstance().getPoint();
                        double score = ComputScoreManager.getInstance().getScore(result);
                        String originaldata = ComputScoreManager.getInstance().getOriginaldata(result);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                originaltext.setText(originaldata);
                                resultValue.setText(score + "m");
                            }
                        });
                    }
                }.start();  //开启一个线程
                break;
            case R.id.makezero:
                new Thread() {
                    public void run() {
                        TotalStationManager.getInstance().makeZero();
                        PointPosition result = TotalStationManager.getInstance().getPoint();
                        ComputScoreManager.getInstance().setThrow_m(result.getDistance());
                    }
                }.start();  //开启一个线程
                break;
        }

    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //22上键
        if (keyCode == 22) {
            TotalStationManager.getInstance().changeLASER_POINTER();
        }
        //22下键
        if (keyCode == 21) {
            TotalStationManager.getInstance().changeLASER_PLUMMET();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void newAngle(Double angel) {
        runOnUiThread(() -> {
            hr_value.setText(AngleUtils.changeToDFMArray(angel));
        });
    }

    @Override
    public void newDistance(Double distance) {

    }
}
